const HOST = window.location.host;
const HOSTNAME = window.location.hostname;
const PORT = window.location.port;
const PROTOCOL = window.location.protocol;
const RABBITMQ_STOMP_PATH = '/JAKE-RABBITMQ-STOMP';
const KAFKA_STOMP_PATH = '/JAKE-KAFKA-STOMP';
const ENDPOINT = '/websocket';

$(function () {
    connect2RabbitMQ();
    connect2Kafka();
})

function connect2RabbitMQ() {
    let sockUrl = PROTOCOL + '//' + HOSTNAME + ":9000" + RABBITMQ_STOMP_PATH + ENDPOINT;
    let sockJS = new SockJS(sockUrl);
    let stompClient = Stomp.over(sockJS);
    stompClient.connect({}, function (frame) {
        indicateOnScreen('已连接：' + frame);
        stompClient.subscribe('/topic/rabbitmq', function (response) {
            indicateOnScreen('rabbitmq: ' + response.body);
        })
    })
}

function connect2Kafka() {
    let sockUrl = PROTOCOL + '//' + HOSTNAME + ":9000" + KAFKA_STOMP_PATH + ENDPOINT;
    let sockJS = new SockJS(sockUrl);
    let stompClient = Stomp.over(sockJS);
    stompClient.connect({}, function (frame) {
        indicateOnScreen('已连接：' + frame);
        stompClient.subscribe('/topic/kafka', function (response) {
            indicateOnScreen('kafka: ' + response.body);
        })
    })
}

//模拟将消息显示在屏幕上
function indicateOnScreen(msg) {
    let screen = $('#screen');
    screen.html(screen.html() + msg + '<br/>')
}